<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\ElectricFee;
use App\Models\GarbageFee;
use App\Models\GasFee;
use App\Models\WaterFee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WaterFeeController extends Controller
{
    /*
     * 水费列表
     */
    public function index(Request $request, WaterFee $waterFee)
    {
        $communityId = $request->input('community_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $res = $waterFee->indexPageLike($request->page, $request->limit, $request->condition, $communityId);
        } else {
            $res = $waterFee->indexPage($request->page, $request->limit, $communityId);
        }
        if ($res && count($res) > 0) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 水费记录数
     */
    public function indexNum(Request $request, WaterFee $waterFee)
    {
        $communityId = $request->input('community_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $condition = $request->condition;
            $query = 'SELECT COUNT(*) as count FROM `water_fee` WHERE `community_id`=? AND`status` = 1 and CONCAT(`house_number`,`username`) LIKE ?';
            $param = '%' . $condition . '%';
            $count = DB::select($query, array($communityId, $param));
            $res = $count[0]->count;
        } else {
            $res = $waterFee->where('status', 1)->count();
        }
        if ($res) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    public function save(Request $request, WaterFee $waterFee, ElectricFee $electricFee, GarbageFee $garbageFee)
    {
        $this->validate($request, [
            'house_number' => 'required',
            'username' => 'required',
            'start_number' => 'required|integer',
            'end_number' => 'required|integer',
            'unit_price' => 'required',
            'cost' => 'required',
            'pay_status' => 'required',
            'consumption' => 'required|integer',
            'cost_type' => 'required|integer',
            'pay_type' => 'required|integer'

        ], [
            'house_number.required' => '房间号不能为空',
            'username.required' => '户主名不能为空',
            'number.integer' => '服务数量必须为数字'
        ]);
        $param = $request->all();

        $param['community_id'] = 1;//所属小区id
        //$param['unit_price'] *= 10;//单价 *10 取 /10
        $param['status'] = 1;

        if ($param['cost_type'] == 1) {//添加水费
            $param['water_number'] = 'A123456';//水费表编号
            $code = $waterFee->create($param);
        } elseif ($param['cost_type'] == 2) {//添加电费
            $param['electric_number'] = 'A123456';//电费表编号
            $code = $electricFee->create($param);
        } elseif ($param['cost_type'] == 3) {//添加气费
            $param['gas_number'] = 'A123456';//气费表编号
            $code = $garbageFee->create($param);
        }
        if ($code) {
            return $this->return_json(200, '', '添加成功');
        } else {
            return $this->return_json(201, '', '添加失败');
        }
    }

    public function update(Request $request, WaterFee $waterFee, ElectricFee $electricFee, GasFee $gasFee)
    {
        $this->validate($request, [
            'id' => 'required',
            'cost_type' => 'required|integer'
        ], [
            'id.required' => 'id不能为空',
            'cost_type.required' => '费用类型不能为空',
            'cost_type.integer' => '费用类型必须是数字'
        ]);
        $param = $request->all();
        if ($param['cost_type'] == 1) {//渲染修改水费页面
            $code = $waterFee->whereid($request->id)->update($param);
        } elseif ($param['cost_type'] == 2) {//添加电费
            $code = $electricFee->whereid($request->id)->update($param);
        } elseif ($param['cost_type'] == 3) {//添加气费
            $code = $gasFee->whereid($request->id)->update($param);
        }
        if ($code) {
            return $this->return_json(200, '', '修改成功');
        } else {
            return $this->return_json(201, '', '修改失败');
        }
    }

    public function delete(Request $request, WaterFee $waterFee)
    {
        $this->validate($request, [
            'id' => 'required'
        ], [
            'id.required' => 'id不能为空'
        ]);
        $param = $request->all();
        $param['status'] = 0;
        $code = $waterFee->whereid($request->id)->update($param);
        if ($code) {
            return $this->return_json(200, '', '删除成功');
        } else {
            return $this->return_json(201, '', '删除失败');
        }
    }

    /*
     * 渲染修改页面
     */
    public function getWaterById(Request $request, WaterFee $waterFee, ElectricFee $electricFee, GasFee $gasFee)
    {
        $this->validate($request, [
            'id' => 'required',
            'cost_type' => 'required|integer'
        ], [
            'id.required' => 'id不能为空',
            'cost_type.required' => '费用类型不能为空',
            'cost_type.integer' => '费用类型必须是数字'
        ]);
        $param = $request->all();
        if ($param['cost_type'] == 1) {//渲染修改水费页面
            $res = $waterFee->whereid($request->id)->get();
        } elseif ($param['cost_type'] == 2) {//添加电费
            $res = $electricFee->whereid($request->id)->get();
        } elseif ($param['cost_type'] == 3) {//添加气费
            $res = $gasFee->whereid($request->id)->get();
        }
        if ($res) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败,数据为空');
        }
    }
}
